/*===========================================================
 
 .---=||||[ B L O O D Y ]|||||=====================----------....
 |                                                              |
 |    THE  M O S T   B L O O D Y  GAME EVER!                    |
 |                                                              |
 |    bloody harvest is coming... soon,                         |
 |                                              heil Satan!     |
 !                                                              !
 .                                                              ,
  ~~~~-------------================================|||||||||==-~




 Name-Space: core
 Module: exceptions
 Purpose: Provide basic exception system
 Last modification:  barzto, 07/05/2006

 TODO:

 FIX:

 
 
 ============================================================*/

#include <cstdio>
#include <cstdarg>
#include <cstdlib>
#include <cstring>
#include "core/exceptions.h"

namespace core
{
	//-------------------------------------------------------------------------
	// 
	// BaseException class implementation
	// 
	//-------------------------------------------------------------------------

	BaseException::BaseException(const char* fmt, ...) throw()
	: message(0)
	{
		va_list args;
		va_start(args,fmt);
		message = new char[256];
		vsnprintf(message, 256, fmt, args);
		va_end(args);
	}

	BaseException::BaseException(const BaseException& src)
	: exception(), message(0)
	{
		message = strdup(src.message);
	}

	BaseException& BaseException::operator=(const BaseException& rhs)
	{
		if (message)
		{
			free(message);
		}
		message = strdup(rhs.message);

		return *this;
	}

	BaseException::~BaseException() throw()
	{
		free(message);
	}


	//-------------------------------------------------------------------------
	// 
	// OutOfRangeException class implementation
	// 
	//-------------------------------------------------------------------------
	OutOfRangeException::OutOfRangeException(u32 rgMin, u32 rgMax, u32 curVal)
		: BaseException("Value %d gets out of range [%d, %d]", curVal, rgMin, rgMax),
		rangeMin(rgMin), rangeMax(rgMax), currentVal(curVal)
	{
	}

	OutOfRangeException::OutOfRangeException(const OutOfRangeException& src)
		: BaseException(src), rangeMin(src.rangeMin), rangeMax(src.rangeMax),
		currentVal(src.currentVal)
	{
	}

	OutOfRangeException& OutOfRangeException::operator=(const OutOfRangeException& rhs)
	{
		if (message)
		{
			free(message);
		}

		message = strdup(rhs.message);
		rangeMin = rhs.rangeMin;
		rangeMax = rhs.rangeMax;
		currentVal = rhs.currentVal;

		return *this;
	}

	//-------------------------------------------------------------------------
	// 
	// FileNotFoundException class implementation
	// 
	//-------------------------------------------------------------------------

	FileNotFoundException::FileNotFoundException(const char* file)
	: BaseException("File %s not found", file), fileName(file)
	{
	}

	FileNotFoundException::FileNotFoundException(const FileNotFoundException& src)
	: BaseException(src),fileName(src.fileName)
	{
	}

	FileNotFoundException& FileNotFoundException::operator=(const FileNotFoundException& rhs)
	{
		if (message)
		{
			free(message);
		}

		message = strdup(rhs.message);
		fileName = rhs.fileName;

		return *this;
	}

}

